#include "Executor.h"
#include "InternalCommands.h"
#include "MyShellGlobalVariables.h"
#include "MyShellAssistFunctions.h"
#include "MyShellException.h"

#include <iostream>
#include <cstring>
#include <thread>
#include <unordered_set>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/time.h>

// 执行内部命令（根据内部命令id进入不同的函数进行执行）
void mysh::Executor::executeInternalCommands(const std::uint32_t& internal_command_id, const std::vector<std::string>& splited_command)
{
    std::cerr << "\033[32m" << "运行内部命令：" << "\033[0m" << std::endl;
    switch (internal_command_id)
    {
    case 0:
        mysh::myExit(splited_command);
        break;
    case 1:
        mysh::myChangeSkip(splited_command);
        break;
    case 2:
        mysh::myLS(splited_command);
        break;
    case 3:
        mysh::myCD(splited_command);
        break;
    case 4:
        mysh::myAlias(splited_command);
        break;
    case 5:
        mysh::myHistory(splited_command);
        break;
    case 6:
        mysh::myType(splited_command);
        break;
    case 7:
        mysh::myRefresh(splited_command);
        break;
    case 8:
        mysh::yourYeq(splited_command);
        break;
    case 9:
        mysh::myHelp(splited_command);
        break;
    case 10:
        mysh::mySh(splited_command);
        break;
    }
}

// 执行外部命令
void mysh::Executor::executeExternalCommands(std::vector<std::string>& splited_command)
{
    std::cerr << "\033[32m" << "运行外部命令：" << "\033[0m" << std::endl;
    // 补全外部命令路径
    splited_command[0] = (mysh::external_commands_path + "/" + splited_command[0]);
    // 将splited_command复制到二维动态字符数组args中，方便传递给execv
    char** args = new char*[splited_command.size() + 1];
    for (std::size_t i = 0; i < splited_command.size(); i++)
    {
        args[i] = new char[splited_command[i].size() + 1];
        std::strcpy(args[i], splited_command[i].c_str());
    }
    args[splited_command.size()] = nullptr;
    // 创建子进程
    pid_t pid = fork();
    // 子进程中执行命令
    if (pid == 0)
    {
        if (execv(args[0], args) == -1)
        {
            std::cerr << "\033[31m" << splited_command[0] << "运行失败！" << "\033[0m" << std::endl;
            std::cerr << "\033[31m" << "错误信息：" << strerror(errno) << "\033[0m" << std::endl;
            exit(1);
        }
    }
    // 父进程等待子进程执行
    else if (pid > 0)
    {
        int status;
        waitpid(pid, &status, 0);
    }
    else
    {
        std::cerr << "\033[31m" << splited_command[0] << "运行失败！" << "\033[0m" << std::endl;
    }
    // 释放内存
    for (std::size_t i = 0; i < splited_command.size(); i++)
    {
        delete[] args[i];
    }
    delete[] args;
}

// 执行系统命令
void mysh::Executor::executeSystemCommands(std::vector<std::string>& splited_command)
{
    std::cerr << "\033[32m" << "运行系统命令：" << "\033[0m" << std::endl;
    // 将splited_command复制到二维动态字符数组args中，方便传递给execvp
    char** args = new char*[splited_command.size() + 1];
    for (std::size_t i = 0; i < splited_command.size(); i++)
    {
        args[i] = new char[splited_command[i].size() + 1];
        std::strcpy(args[i], splited_command[i].c_str());
    }
    args[splited_command.size()] = nullptr;
    // 创建子进程
    pid_t pid = fork();
    // 子进程中执行命令
    if (pid == 0)
    {
        if (execvp(args[0], args) == -1)
        {
            std::cerr << "\033[31m" << splited_command[0] << "运行失败！" << "\033[0m" << std::endl;
            std::cerr << "\033[31m" << "错误信息：" << strerror(errno) << "\033[0m" << std::endl;
            exit(1);
        }
    }
    // 父进程等待子进程执行完成
    else if (pid > 0)
    {
        int status;
        waitpid(pid, &status, 0);
    }
    else
    {
        std::cerr << "\033[31m" << splited_command[0] << "运行失败！" << "\033[0m" << std::endl;
    }
    // 释放内存
    for (std::size_t i = 0; i < splited_command.size(); i++)
    {
        delete[] args[i];
    }
    delete[] args;
}

// 执行管道命令
void mysh::Executor::executePipe(std::vector<std::string>& splited_command)
{
    std::cerr << "\033[32m" << "运行管道命令：" << "\033[0m" << std::endl;
    std::size_t splited_n = splited_command.size();
    // 二维字符串数组，每一列都是一个已分割的命令
    std::vector<std::vector<std::string>> commands;
    // 辅助获取每一条已分割的命令
    std::vector<std::string> every_command;
    // 获取被”|“分开的每一条已分割命令
    for (std::int32_t i = 0; i < splited_n; i++)
    {
        if (splited_command[i] == "|")
        {
            if (!every_command.empty())
            {
                commands.push_back(every_command);
                every_command.clear();
            }
        }
        else
        {
            every_command.push_back(splited_command[i]);
        }
    }
    if (!every_command.empty())
    {
        commands.push_back(every_command);
    }
    // 命令数量
    std::size_t n = commands.size();
    // 原始标准输入的文件描述符
    int origin_stdin = dup(STDIN_FILENO);
    // 原始标准输出的文件描述符
    int origin_stdout = dup(STDOUT_FILENO);
    // 用于辅助创建管道
    int pipefd_assist[2] = {0};
    // 所有管道的文件描述符
    std::vector<std::vector<int>> pipefds = std::vector<std::vector<int>>(n - 1, std::vector<int>(2));
    // 创建n-1个管道，任意一个创建失败直接退出执行
    for (std::int32_t i = 0; i < n - 1; i++)
    {
        if (pipe(pipefd_assist) == -1)
        {
            std::cerr << "\033[31m" << "创建管道失败" << "\033[0m" << std::endl;
            return;
        }
        pipefds[i][0] = pipefd_assist[0];
        pipefds[i][1] = pipefd_assist[1];
    }
    // 逐步执行每一条命令，并使相邻命令之间通过管道进行通信
    for (std::int32_t i = 0; i < n; i++)
    {
        // 命令执行者编号，0表示系统命令，1表示内部命令，2表示外部命令
        std::int32_t cur_command_executor = 0;
        // 用于将当前已分割命令转为动态字符串二维数组
        char** args = nullptr;
        // 获取命令执行者编号
        if (mysh::internal_commands.count(commands[i][0]))
        {
            cur_command_executor = 1;
        }
        else if (mysh::external_commands.count(commands[i][0]))
        {
            std::cerr << "\033[32m" << "运行外部命令：" << "\033[0m" << std::endl;
            cur_command_executor = 2;
            commands[i][0] = (mysh::external_commands_path + "/" + commands[i][0]);
        }
        else
        {
            std::cerr << "\033[32m" << "运行系统命令：" << "\033[0m" << std::endl;
        }
        // 当前要执行的命令不是内部命令，将当前已分割命令转为动态字符串二维数组
        if (cur_command_executor != 1)
        {
            args = new char*[commands[i].size() + 1];
            for (std::size_t j = 0; j < commands[i].size(); j++)
            {
                args[j] = new char[commands[i][j].size() + 1];
                std::strcpy(args[j], commands[i][j].c_str());
            }
            args[commands[i].size()] = nullptr;
        }
        // 创建子进程
        pid_t pid = fork();
        // 子进程
        if (pid == 0)
        {
            // 将当前标准输入重定向到上一个管道的读端，关闭上一个管道的写端
            if (i > 0)
            {
                dup2(pipefds[i - 1][0], STDIN_FILENO);
                close(pipefds[i - 1][1]);
            }
            // 将当前标准输出重定向到当前管道的写端，关闭当前管道的读端
            if (i < n - 1)
            {
                dup2(pipefds[i][1], STDOUT_FILENO);
                close(pipefds[i][0]);
            }
            // 执行命令
            switch (cur_command_executor)
            {
            case 0:
                // 系统命令
                if (execvp(args[0], args) == -1)
                {
                    std::cerr << "\033[31m" << commands[i][0] << "运行失败！" << "\033[0m" << std::endl;
                    std::cerr << "\033[31m" << "错误信息：" << strerror(errno) << "\033[0m" << std::endl;
                    exit(1);
                }
                break;
            case 1:
                // 内部命令
                mysh::Executor::executeInternalCommands(mysh::internal_commands[commands[i][0]], commands[i]);
                exit(1);
                break;
            case 2:
                // 外部命令
                if (execv(args[0], args) == -1)
                {
                    std::cerr << "\033[31m" << commands[i][0] << "运行失败！" << "\033[0m" << std::endl;
                    std::cerr << "\033[31m" << "错误信息：" << strerror(errno) << "\033[0m" << std::endl;
                    exit(1);
                }
                break;
            }
        }
        // 父进程
        else if (pid > 0)
        {
            // 等待子进程结束并关闭上一个管道的读端和当前管道的写端
            int status;
            waitpid(pid, &status, 0);
            if (i > 0)
            {
                close(pipefds[i - 1][0]);
            }
            if (i < n - 1)
            {
                close(pipefds[i][1]);
            }
        }
        else if (pid < 0)
        {
            std::cerr << "\033[31m" << commands[i][0] << "运行失败！" << "\033[0m" << std::endl;
        }
        // 释放内存
        if (args != nullptr)
        {
            for (std::size_t j = 0; j < commands[i].size(); j++)
            {
                delete[] args[j];
            }
            delete[] args;
        }
    }
    // 等待所有进程结束
    wait(NULL);
    // 关闭所有管道并将标准输入和标准输出重定向到原来的位置
    for (std::int32_t i = 0; i < n - 1; i++)
    {
        close(pipefds[i][0]);
        close(pipefds[i][1]);
    }
    dup2(origin_stdin, STDIN_FILENO);
    dup2(origin_stdout, STDOUT_FILENO);
    close(origin_stdin);
    close(origin_stdout);
}

// 执行别名、变量赋值命令，与内部命令alias有区别
void mysh::Executor::executeAlias(std::vector<std::string>& splited_command)
{
    // 等号数量
    std::int32_t equ_count = 0;
    // 等号前的字符串（不含跳过字符）
    std::string pre = std::string();
    // 等号后的字符串（含跳过字符）
    std::string suf = std::string();
    // 获取pre及suf，若发现等号数量大于1，则退出循环
    for (std::int32_t i = 0; i < splited_command.size(); i++)
    {
        if (splited_command[i] == "=")
        {
            equ_count++;
            if (equ_count > 1)
            {
                break;
            }
            continue;
        }
        if (equ_count == 0)
        {
            pre += splited_command[i];
        }
        else
        {
            suf += splited_command[i] + mysh::default_skip_char;
        }
    }
    // 等号前或等号后无字符串，向用户报告错误
    if (pre.size() == 0 || suf.size() == 0)
    {
        std::cerr << "\033[31m" << "请检查等号前后是否有字符串" << "\033[0m" << std::endl;
        return;
    }
    // 删除等号后字符串的跳过字符
    suf.pop_back();
    // 等号数量为1，pre不等于suf的情况下才保存这个别名（或变量）
    if (equ_count == 1)
    {
        if (pre == suf)
        {
            std::cerr << "\033[31m" << "出现循环赋值错误！" << "\033[0m" << std::endl;
            return;
        }
        mysh::alias[pre] = suf;
        std::cerr << "\033[32m" << "取别名(或变量赋值)成功！" << "\033[0m" << std::endl; 
    }
    // 等号数量不为1
    else
    {
        std::cerr << "\033[31m" << "等号过多，无法取别名(或变量赋值)！" << "\033[0m" << std::endl;
    }
}

// 执行一行命令
void mysh::Executor::executeLineCommand(const std::string& command)
{
    // 用于保存分割后的命令
    static std::vector<std::string> splited_command = std::vector<std::string>();
    // 清空分割后的命令
    splited_command.clear();
    // 刷新资源
    // 上一次到达此步的微秒数
    static unsigned long long last_time = 0;
    // 延迟（距上一次到达此步）
    static unsigned long long delay = 0;
    // 多线程，用于异步执行刷新函数
    static std::vector<std::thread> threads_to_refresh;
    // 获取当前微秒数
    struct timeval time_val;
    gettimeofday(&time_val, NULL);
    unsigned long long cur_time = time_val.tv_sec * 1e6 + time_val.tv_usec;
    // 计算延迟
    delay = cur_time - last_time;
    // 延迟大于mysh::refresh_delay微秒，刷新资源，并将当前时间保存为上次时间
    if (delay > mysh::refresh_delay)
    {
        last_time = cur_time;
        threads_to_refresh.push_back(std::thread(mysh::refreshResource));
        threads_to_refresh.push_back(std::thread(mysh::refreshInfo));
    }
    // 是否输入重定向置为否
    mysh::if_input_redirection = false;
    // 用于输入重定向的文件名置空
    mysh::file_to_input_redirection.clear();
    // 是否输出重定向置为否
    mysh::if_output_redirection = false;
    // 用于输出重定向的文件名置空
    mysh::file_to_output_redirection.clear();
    // 输入命令
    std::string cur_command = command;
    // 用于辅助分割命令的数组
    static std::vector<std::string> assist_splited_command = std::vector<std::string>();
    // 清空辅助分割命令数组后，对命令进行初步分割
    assist_splited_command.clear();
    assist_splited_command = std::vector<std::string>(std::move(mysh::split(cur_command, default_skip_char)));
    // 用于辅助获取输入重定向文件名的bool值
    bool if_reinput = false;
    // 用于辅助获取输出重定向文件名的bool值
    bool if_reoutput = false;
    // 二次分割，分割前进行字符串替换，同时获取输入输出重定向的文件名
    for (std::int32_t i = 0; i < assist_splited_command.size(); i++)
    {
        // 字符串替换
        mysh::replaceString(assist_splited_command[i]);
        // 分割当前被替换字符串
        std::vector<std::string> assist = std::vector<std::string>(std::move(mysh::split(assist_splited_command[i], default_skip_char)));
        // 遍历assist数组，获取输入输出重定向文件名，并将数组中的每个值（除了与输入输出重定向相关的值）添加到splited_command末尾
        for (std::int32_t j = 0; j < assist.size(); j++)
        {
            // 指定输入重定向
            if (assist[j] == "<")
            {
                mysh::if_input_redirection = true;
                if_reinput = true;
                continue;
            }
            // 指定输出重定向
            else if (assist[j] == ">")
            {
                mysh::if_output_redirection = true;
                if_reoutput = true;
                continue;
            }
            // 辅助字符串是“|”，则当前命令为管道命令
            else if (assist[j] == "|")
            {
                mysh::if_pipe = true;
            }
            // 辅助字符串是“=”，则当前命令为赋值命令
            else if (assist[j] == "=")
            {
                mysh::if_alias = true;
            }
            // 获取输入重定向文件名
            if (if_reinput)
            {
                mysh::file_to_input_redirection = assist[j];
                if_reinput = false;
            }
            // 获取输出重定向文件名
            else if (if_reoutput)
            {
                mysh::file_to_output_redirection = assist[j];
                if_reoutput = false;
            }
            // 其他值添加到splited_command末尾
            else
            {
                splited_command.push_back(assist[j]);
            }
        }
    }
    // 等待资源刷新线程结束
    if (delay > mysh::refresh_delay)
    {
        for (std::int32_t i = 0; i < threads_to_refresh.size(); i++)
        {
            threads_to_refresh[i].join();
        }
        threads_to_refresh.clear();
    }
    // 判断当前命令是否需要后台运行
    if (!splited_command.empty() && splited_command.back() == "&")
    {
        // 删除分割后命令末尾的&
        splited_command.pop_back();
        // 创建子进程
        pid_t background_pid = fork();
        // 父进程输出子进程的进程id
        if (background_pid > 0)
        {
            std::cerr << "\033[32m" << "后台运行进程id为: " << background_pid << "\033[0m" << std::endl;
            return;
        }
        // 创建子进程失败
        else if (background_pid < 0)
        {
            std::cerr << "\033[31" << "无法创建子进程以开启新会话" << "\033[0m" << std::endl;
            return;
        }
        // 子进程开启新会话，关闭标准输入、标准输出、标准错误，并将if_in_background置为真，表示当前进程处于后台
        else
        {
            setsid();
            close(STDERR_FILENO);
            close(STDIN_FILENO);
            close(STDOUT_FILENO);
            mysh::if_in_background = true;
        }
    }
    // splited_command不空，则进入此分支
    if (!splited_command.empty())
    {
        // 备份标准输入输出的文件描述符
        int origin_std_input = dup(STDIN_FILENO);
        int origin_std_output = dup(STDOUT_FILENO);
        // 用于重定向标准输入输出的文件描述符
        int fd_input = -1;
        int fd_output = -1;
        // 需要重定向则进入次分支
        if (mysh::if_input_redirection || mysh::if_output_redirection)
        {
            // 输入重定向及输出重定向所用的文件相同，不进行重定向
            if (mysh::if_input_redirection && mysh::if_output_redirection && mysh::file_to_input_redirection == mysh::file_to_output_redirection)
            {
                std::cerr << "\033[31m" << "当前重定向输入的文件和重定向输出的文件相同，不可重定向" << "\033[0m" << std::endl;
                mysh::if_input_redirection = false;
                mysh::if_output_redirection = false;
                goto out;
            }
            // 输入重定向
            if (mysh::if_input_redirection)
            {
                // 尝试打开输入重定向文件
                fd_input = open(mysh::file_to_input_redirection.c_str(), O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
                if (fd_input == -1)
                {
                    std::cerr << "\033[31m" << "无法打开文件: " << mysh::file_to_input_redirection << ", 错误信息: " << std::strerror(errno) << std::endl;
                    mysh::if_input_redirection = false;
                }
                // 尝试重定向
                else if (dup2(fd_input, STDIN_FILENO) == -1)
                {
                    mysh::if_input_redirection = false;
                    std::cerr << "输入重定向失败" << std::endl;
                }
            }
            // 输出重定向
            if (mysh::if_output_redirection)
            {
                // 尝试打开输出重定向文件
                fd_output = open(mysh::file_to_output_redirection.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IWUSR | S_IWGRP | S_IWOTH);
                if (fd_output == -1)
                {
                    mysh::if_output_redirection = false;
                    std::cerr << "\033[31m" << "无法打开文件: " << mysh::file_to_output_redirection << ", 错误信息: " << std::strerror(errno) << std::endl;
                }
                // 尝试重定向
                else if (dup2(fd_output, STDOUT_FILENO) == -1)
                {
                    mysh::if_output_redirection = false;
                    std::cerr << "输出重定向失败" << std::endl;
                }
            }
        }
        // 执行命令
        out:
        // 执行普通命令
        if (!mysh::if_pipe && !mysh::if_alias)
        {
            if (mysh::internal_commands.count(splited_command[0]))
            {
                mysh::Executor::executeInternalCommands(mysh::internal_commands[splited_command[0]], splited_command);
            }
            else if (mysh::external_commands.count(splited_command[0]))
            {
                mysh::Executor::executeExternalCommands(splited_command);
            }
            else
            {
                mysh::Executor::executeSystemCommands(splited_command);
            }
        }
        // 执行管道命令
        else if (mysh::if_pipe)
        {
            mysh::Executor::executePipe(splited_command);
            mysh::if_pipe = false;
        }
        // 执行别名命名(或变量赋值)命令
        else if (mysh::if_alias)
        {
            mysh::Executor::executeAlias(splited_command);
            mysh::if_alias = false;
        }
        // 若重定向，则恢复原标准输入输出的文件描述符
        if (mysh::if_input_redirection || mysh::if_output_redirection)
        {
            if (fd_input != -1)
            {
                close(fd_input);
                dup2(origin_std_input, STDIN_FILENO);
                close(origin_std_input);
                fd_input = -1;
            }
            if (fd_output != -1)
            {
                close(fd_output);
                dup2(origin_std_output, STDOUT_FILENO);
                close(origin_std_output);
                fd_output = -1;
            }
            mysh::if_input_redirection = false;
            mysh::file_to_input_redirection.clear();
            mysh::if_output_redirection = false;
            mysh::file_to_output_redirection.clear();
        }
    }
    // 若当前进程处于后台，则命令执行完成后退出当前进程
    if (mysh::if_in_background)
    {
        mysh::if_in_background = false;
        exit(0);
    }
}

// 在一个作用域上执行一系列语句
void mysh::MyShellScriptExecutor::executeSeriesStatements(mysh::MyShellActionScope& action_scope, const std::vector<std::shared_ptr<mysh::GrammarNode>>& grammar_nodes)
{
    // 当前作用域的声明
    std::unordered_set<std::string> declares_of_current_action_scope = std::unordered_set<std::string>();
    for (auto node : grammar_nodes)
    {
        // 声明语句
        if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::DECLARE_STATEMENT)
        {
            std::vector<std::string> cur_identifiers = mysh::MyShellScriptExecutor::executeDeclareStatement(action_scope, node);
            for (auto identifier : cur_identifiers)
            {
                declares_of_current_action_scope.insert(identifier);
            }
        }
        // 运行语句
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::RUN_STATEMENT)
        {
            mysh::MyShellScriptExecutor::executeRunStatement(action_scope, node);
        }
        // 输入语句
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::INPUT_STATEMENT)
        {
            mysh::MyShellScriptExecutor::executeInputStatement(action_scope, node);
        }
        // 输出语句
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::OUTPUT_STATEMENT)
        {
            mysh::MyShellScriptExecutor::executeOutputStatement(action_scope, node);
        }
        // 赋值语句
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::ASSIGN_STATEMENT)
        {
            mysh::MyShellScriptExecutor::executeAssignStatement(action_scope, node);
        }
        // if-else语句块
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::IF_ELSE_BLOCK)
        {
            mysh::MyShellScriptExecutor::executeIfElseStatementBlock(action_scope, node);
        }
        // for-in语句块
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::FOR_IN_BLOCK)
        {
            mysh::MyShellScriptExecutor::executeForInStatementBlock(action_scope, node);
        }
        // for-to语句块
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::FOR_TO_BLOCK)
        {
            mysh::MyShellScriptExecutor::executeForToStatementBlock(action_scope, node);
        }
    }
    // 删除当前作用域下新声明的标识符及其数据
    for (auto identifier : declares_of_current_action_scope)
    {
        if (action_scope.declared_variables.count(identifier))
        {
            action_scope.declared_variables.erase(identifier);
        }
        if (action_scope.bool_array_values.count(identifier))
        {
            action_scope.bool_array_values.erase(identifier);
        }
        if (action_scope.bool_values.count(identifier))
        {
            action_scope.bool_values.erase(identifier);
        }
        if (action_scope.int_array_values.count(identifier))
        {
            action_scope.int_array_values.erase(identifier);
        }
        if (action_scope.int_values.count(identifier))
        {
            action_scope.int_values.erase(identifier);
        }
        if (action_scope.string_array_values.count(identifier))
        {
            action_scope.string_array_values.erase(identifier);
        }
        if (action_scope.string_values.count(identifier))
        {
            action_scope.string_values.erase(identifier);
        }
    }
}

// 执行声明语句
std::vector<std::string> mysh::MyShellScriptExecutor::executeDeclareStatement(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node)
{
    auto declare_node = std::static_pointer_cast<mysh::DeclareStatementNode>(node);
    action_scope.addDeclaredVariables(declare_node);
    return declare_node->identifiers;
}

// 执行运行语句
void mysh::MyShellScriptExecutor::executeRunStatement(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node)
{
    auto run_node = std::static_pointer_cast<mysh::RunStatementNode>(node);
    std::string command = run_node->command;
    // 有参数列表
    if (!run_node->param_list.empty())
    {
        // 辅助进行字符串替换
        std::string assist = std::string();
        // 命令字符串索引
        std::int32_t i = 0;
        // 参数列表索引
        std::int32_t j = 0;
        // 命令字符串长度
        std::size_t n = run_node->command.size();
        // 参数列表大小
        std::size_t m = run_node->param_list.size();
        while (i < n)
        {
            if (run_node->command[i] != '#' || j == m)
            {
                assist += run_node->command[i];
            }
            else
            {
                mysh::Word cur_param = run_node->param_list[j];
                j++;
                if (cur_param.word_type == mysh::WORD_TYPE::IDENTIFIER)
                {
                    if (!action_scope.declared_variables.count(cur_param.name))
                    {
                        throw mysh::ExecutorException("\033[31m运行语句的参数列表中存在尚未声明的变量\033[0m");
                    }
                    std::string add_string = std::string();
                    switch (action_scope.declared_variables[cur_param.name].data_type)
                    {
                    case mysh::WORD_TYPE::BOOL_VALUE:
                        if (!action_scope.bool_values.count(cur_param.name))
                        {
                            throw mysh::ExecutorException("\033[31m运行语句的参数列表中的变量" + cur_param.name + "尚未初始化\033[0m");
                        }
                        if (action_scope.bool_values[cur_param.name])
                        {
                            add_string = "true";
                        }
                        else
                        {
                            add_string = "false";
                        }
                        break;
                    case mysh::WORD_TYPE::INTEGER:
                        if (!action_scope.int_values.count(cur_param.name))
                        {
                            throw mysh::ExecutorException("\033[31m运行语句的参数列表中的变量" + cur_param.name + "尚未初始化\033[0m");
                        }
                        add_string = std::to_string(action_scope.int_values[cur_param.name]);
                        break;
                    case mysh::WORD_TYPE::STRING_VALUE:
                        if (!action_scope.string_values.count(cur_param.name))
                        {
                            throw mysh::ExecutorException("\033[31m运行语句的参数列表中的变量" + cur_param.name + "尚未初始化\033[0m");
                        }
                        add_string = action_scope.string_values[cur_param.name];
                        break;
                    default:
                        throw mysh::ExecutorException("\033[31m运行语句的参数列表数据类型不符合预期\033[0m");
                        break;
                    }
                    assist += add_string;
                }
            }
            i++;
        }
        command = assist;
    }
    // 若返回类型为void，则表示不捕获标准输出中的数据，可直接执行命令
    if (run_node->data_type == mysh::WORD_TYPE::VOID)
    {
        // 执行命令
        mysh::Executor::executeLineCommand(command);
        return;
    }
    // 备份标准输出文件描述符
    int original_stdout = dup(STDOUT_FILENO);
    int original_stdin = dup(STDIN_FILENO);
    // 创建管道
    int io_pipe[2] = {0};
    if (pipe(io_pipe) == -1)
    {
        throw mysh::ExecutorException("\033[31m无法创建管道以获取运行语句的返回值\033[0m");
    }
    // 存储运行语句的返回值
    std::string return_data = std::string();
    // 创建子进程捕获运行语句返回值
    pid_t pid = fork();
    if (pid < 0)
    {
        throw mysh::ExecutorException("\033[31m无法创建子进程以捕获运行语句的返回值\033[0m");
    }
    // 子进程
    else if (pid == 0)
    {
        // 标准输出重定向到写端并关闭读端
        dup2(io_pipe[1], STDOUT_FILENO);
        close(io_pipe[0]);
        // 执行命令
        mysh::Executor::executeLineCommand(command);
        close(io_pipe[1]);
        exit(0);
    }
    // 父进程
    else
    {
        // 标准输入重定向到读端并关闭写端
        close(io_pipe[1]);
        dup2(io_pipe[0], STDIN_FILENO);
        // 获取返回值
        char buf[1024] = {0};
        while (read(STDIN_FILENO, buf, sizeof(buf) - 1) > 0)
        {
            return_data += buf;
        }
        // 等待子进程结束
        int status;
        waitpid(pid, &status, 0);
    }
    // 去除换行符
    while (!return_data.empty() && return_data.back() == '\n')return_data.pop_back();
    if (run_node->data_type == mysh::WORD_TYPE::BOOL_VALUE)
    {
        action_scope.register_bool_return = false;
        // 返回值中发现任意一个非“空”字符，则布尔寄存器变为真
        for (const char& ch : return_data)
        {
            if (ch != '0' && ch != ' ' && ch != '\n' && ch != '\r')
            {
                action_scope.register_bool_return = true;
                break;
            }
        }
        // 大写字母转小写
        for (char& ch : return_data)
        {
            // 大写字母转小写
            if (ch >= 'A' && ch <= 'Z')
            {
                ch += 32;
            }
        }
        // 二次判断
        if (return_data == "false")
        {
            action_scope.register_bool_return = false;
        }
    }
    else if (run_node->data_type == mysh::WORD_TYPE::INTEGER)
    {
        try
        {
            action_scope.register_int_return = static_cast<std::int32_t>(std::stod(return_data));
        }
        catch (const std::exception& e)
        {
            dup2(original_stdin, STDIN_FILENO);
            dup2(original_stdout, STDOUT_FILENO);
            close(original_stdin);
            close(original_stdout);
            close(io_pipe[0]);
            close(io_pipe[1]);
            throw mysh::ExecutorException("\033[31m无法将运行语句的输出转化为整型数据作为返回结果\033[0m");
        }
    }
    else if (run_node->data_type == mysh::WORD_TYPE::STRING_VALUE)
    {
        action_scope.register_string_return = return_data;
    }
    // 恢复标准输出
    dup2(original_stdin, STDIN_FILENO);
    dup2(original_stdout, STDOUT_FILENO);
    close(original_stdin);
    close(original_stdout);
    close(io_pipe[0]);
    close(io_pipe[1]);
}

// 执行输入语句
void mysh::MyShellScriptExecutor::executeInputStatement(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node)
{
    auto input_node = std::static_pointer_cast<mysh::InputOutputStatementNode>(node);
    // 判断是否已声明标识符
    if (!action_scope.declared_variables.count(input_node->identifier))
    {
        throw mysh::ExecutorException("\033[31m执行输入语句之前尚未声明变量" + input_node->identifier + "\033[0m");
    }
    // identifier的值只能是0维数据
    if (action_scope.declared_variables[input_node->identifier].dimension != 0)
    {
        throw mysh::ExecutorException("\033[31m输入语句所指定的标识符的值只能是0维数据\033[0m");
    }
    // 辅助输入
    std::string assist = std::string();
    std::cerr << "\033[35m" << "input: " << "\033[0m" << std::endl;
    std::cin >> assist;
    // 存值
    switch (action_scope.declared_variables[input_node->identifier].data_type)
    {
    case mysh::WORD_TYPE::BOOL_VALUE:
        if (assist == "true" || assist == "1")
        {
            action_scope.bool_values[input_node->identifier] = true;
        }
        else if (assist == "false" || assist == "0")
        {
            action_scope.bool_values[input_node->identifier] = false;
        }
        break;
    case mysh::WORD_TYPE::INTEGER:
        try
        {
            action_scope.int_values[input_node->identifier] = std::stoi(assist);
        }
        catch(const std::exception& e)
        {
            throw mysh::ExecutorException("\033[31m输入语句执行过程中输入的整型数值不正确\033[0m");
        }
        break;
    case mysh::WORD_TYPE::STRING_VALUE:
        action_scope.string_values[input_node->identifier] = assist;
        break;
    }
}

// 执行输出语句
void mysh::MyShellScriptExecutor::executeOutputStatement(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node)
{
    auto output_node = std::static_pointer_cast<mysh::InputOutputStatementNode>(node);
    // 判断是否已声明标识符
    if (!action_scope.declared_variables.count(output_node->identifier))
    {
        throw mysh::ExecutorException("\033[31m执行输入语句之前尚未声明变量" + output_node->identifier + "\033[0m");
    }
    // identifier的值只能是0维数据
    if (action_scope.declared_variables[output_node->identifier].dimension != 0)
    {
        throw mysh::ExecutorException("\033[31m输入语句所指定的标识符的值只能是0维数据\033[0m");
    }
    // 输出
    std::cerr << "\033[35m" << "output: " << "\033[0m" << std::endl;
    switch (action_scope.declared_variables[output_node->identifier].data_type)
    {
    case mysh::WORD_TYPE::BOOL_VALUE:
        if (!action_scope.bool_values.count(output_node->identifier))
        {
            throw mysh::ExecutorException("\033[31m输出尚未定义的变量值\033[0m");
        }
        if (action_scope.bool_values[output_node->identifier])
        {
            std::cerr << "true" << std::endl;
        }
        else
        {
            std::cerr << "false" << std::endl;
        }
        break;
    case mysh::WORD_TYPE::INTEGER:
        if (!action_scope.int_values.count(output_node->identifier))
        {
            throw mysh::ExecutorException("\033[31m输出尚未定义的变量值\033[0m");
        }
        std::cerr << action_scope.int_values[output_node->identifier] << std::endl;
        break;
    case mysh::WORD_TYPE::STRING_VALUE:
        if (!action_scope.string_values.count(output_node->identifier))
        {
            throw mysh::ExecutorException("\033[31m输出尚未定义的变量值\033[0m");
        }
        std::cerr << action_scope.string_values[output_node->identifier] << std::endl;
        break;
    }
}

// 执行赋值语句
void mysh::MyShellScriptExecutor::executeAssignStatement(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node)
{
    // 赋值语句节点
    auto assign_node = std::static_pointer_cast<mysh::AssignStatementNode>(node);
    // 等号左边的语法节点
    auto left_value = assign_node->left_value;
    // 等号左边的语法节点的标识符
    std::string left_value_name = left_value->identifier;
    // 判断等号左边的语法节点的标识符是否已声明
    if (!action_scope.declared_variables.count(left_value_name))
    {
        throw mysh::ExecutorException("\033[31m赋值语句的左值尚未声明\033[0m");
    }
    // 获取左值索引
    std::vector<std::int32_t> left_value_indexes = mysh::MyShellScriptExecutor::parseLeftValueNode(action_scope, left_value);
    // 等号左边的语法节点的数值类型
    mysh::WORD_TYPE left_value_type = action_scope.declared_variables[left_value_name].data_type;
    // 等号右边的表达式
    auto expression = assign_node->expression;
    // 等号右边的表达式的数值类型
    mysh::WORD_TYPE right_value_type = mysh::WORD_TYPE::ILLEGAL_WORD;
    // 等号右边是运行语句
    if (expression->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::RUN_STATEMENT)
    {
        std::shared_ptr<mysh::RunStatementNode> run_expression = std::static_pointer_cast<mysh::RunStatementNode>(expression);
        right_value_type = run_expression->data_type;
        if (right_value_type != left_value_type)
        {
            throw mysh::ExecutorException("\033[31m赋值语句等号左右两边的数值类型不同\033[0m");
        }
        mysh::MyShellScriptExecutor::executeRunStatement(action_scope, run_expression);
    }
    // 等号右边是数值节点
    else if (expression->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::DATA_NODE)
    {
        std::shared_ptr<mysh::DataNode> data_node = std::static_pointer_cast<mysh::DataNode>(expression);
        right_value_type = data_node->data_type;
        if (right_value_type != left_value_type)
        {
            throw mysh::ExecutorException("\033[31m赋值语句等号左右两边的数值类型不同\033[0m");
        }
        mysh::MyShellScriptExecutor::parseDataNode(action_scope, data_node);
    }
    // 等号右边是左值
    else if (expression->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::LEFT_VALUE_NODE)
    {
        std::shared_ptr<mysh::LeftValueNode> left_value_node = std::static_pointer_cast<mysh::LeftValueNode>(expression);
        if (!action_scope.declared_variables.count(left_value_node->identifier))
        {
            throw mysh::ExecutorException("\033[31m赋值语句的右值尚未声明\033[0m");
        }
        else
        {
            right_value_type = action_scope.declared_variables[left_value_node->identifier].data_type;
            if (right_value_type != left_value_type)
            {
                throw mysh::ExecutorException("\033[31m赋值语句等号左右两边的数值类型不同\033[0m");
            }
            std::vector<std::int32_t> indexes = mysh::MyShellScriptExecutor::parseLeftValueNode(action_scope, left_value_node);
            if (!indexes.empty())
            {
                // to do
            }
            else
            {
                if (right_value_type == mysh::WORD_TYPE::INTEGER)
                {
                    if (!action_scope.int_values.count(left_value_node->identifier))
                    {
                        throw mysh::ExecutorException("\033[31m赋值语句右值尚未定义\033[0m");
                    }
                    else
                    {
                        action_scope.register_int_return = action_scope.int_values[left_value_node->identifier];
                    }
                }
                else if (right_value_type == mysh::WORD_TYPE::BOOL_VALUE)
                {
                    if (!action_scope.bool_values.count(left_value_node->identifier))
                    {
                        throw mysh::ExecutorException("\033[31m赋值语句右值尚未定义\033[0m");
                    }
                    else
                    {
                        action_scope.register_bool_return = action_scope.bool_values[left_value_node->identifier];
                    }
                }
                else if (right_value_type == mysh::WORD_TYPE::STRING_VALUE)
                {
                    if (!action_scope.string_values.count(left_value_node->identifier))
                    {
                        throw mysh::ExecutorException("\033[31m赋值语句右值尚未定义\033[0m");
                    }
                    else
                    {
                        action_scope.register_string_return = action_scope.string_values[left_value_node->identifier];
                    }
                }
            }
        }
    }
    else
    {
        // to do
    }
    // 开始赋值
    if (!left_value_indexes.empty())
    {
        // to do
    }
    else
    {
        if (left_value_type == mysh::WORD_TYPE::INTEGER)
        {
            action_scope.int_values[left_value_name] = action_scope.register_int_return;
        }
        else if (left_value_type == mysh::WORD_TYPE::BOOL_VALUE)
        {
            action_scope.bool_values[left_value_name] = action_scope.register_bool_return;
        }
        else if (left_value_type == mysh::WORD_TYPE::STRING_VALUE)
        {
            action_scope.string_values[left_value_name] = action_scope.register_string_return;
        }
    }
}

// 执行if-else语句块
void mysh::MyShellScriptExecutor::executeIfElseStatementBlock(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node)
{
    // if-else语句块
    auto if_else_statement_block = std::static_pointer_cast<mysh::IfElseStatementBlock>(node);
    // 条件数量应该与分支数量相同
    if (if_else_statement_block->conditions.size() != if_else_statement_block->statements.size())
    {
        throw mysh::ExecutorException("\033[31m请检查if-else语句块中条件数量是否与分支数量相同\033[0m");
    }
    // 获取分支编号
    // 条件及分支数量
    std::int32_t n = if_else_statement_block->conditions.size();
    // 分支编号
    std::int32_t block_id = 0;
    // bool寄存器初始化为false
    action_scope.register_bool_return = false;
    while (block_id < n)
    {
        // 当前条件的语法节点类型
        mysh::GRAMMAR_NODE_TYPE cur_condition_type = if_else_statement_block->conditions[block_id]->grammar_node_type;
        // 左值节点
        if (cur_condition_type == mysh::GRAMMAR_NODE_TYPE::LEFT_VALUE_NODE)
        {
            std::shared_ptr<mysh::LeftValueNode> left_value = std::static_pointer_cast<mysh::LeftValueNode>(if_else_statement_block->conditions[block_id]);
            if (!action_scope.declared_variables.count(left_value->identifier))
            {
                throw mysh::ExecutorException("\033[31m当前条件中的左值节点尚未声明\033[0m");
            }
            else
            {
                mysh::WORD_TYPE right_value_type = action_scope.declared_variables[left_value->identifier].data_type;
                std::vector<std::int32_t> indexes = mysh::MyShellScriptExecutor::parseLeftValueNode(action_scope, left_value);
                if (!indexes.empty())
                {
                    // to do
                }
                else
                {
                    if (right_value_type == mysh::WORD_TYPE::BOOL_VALUE)
                    {
                        if (!action_scope.bool_values.count(left_value->identifier))
                        {
                            throw mysh::ExecutorException("\033[31m当前条件中的左值节点尚未定义\033[0m");
                        }
                        else
                        {
                            action_scope.register_bool_return = action_scope.bool_values[left_value->identifier];
                        }
                    }
                    else
                    {
                        throw mysh::ExecutorException("\033[31m当前条件中的左值节点数据类型不是布尔型\033[0m");
                    }
                }
            }
        }
        // 数值节点
        else if (cur_condition_type == mysh::GRAMMAR_NODE_TYPE::DATA_NODE)
        {
            std::shared_ptr<mysh::DataNode> data_node = std::static_pointer_cast<mysh::DataNode>(if_else_statement_block->conditions[block_id]);
            mysh::MyShellScriptExecutor::parseDataNode(action_scope, data_node);
        }
        // 运行语句
        else if (cur_condition_type == mysh::GRAMMAR_NODE_TYPE::RUN_STATEMENT)
        {
            std::shared_ptr<mysh::RunStatementNode> run_expression = std::static_pointer_cast<mysh::RunStatementNode>(if_else_statement_block->conditions[block_id]);
            mysh::MyShellScriptExecutor::executeRunStatement(action_scope, run_expression);
        }
        else
        {
            // to do
        }
        // 找到要进入的分支
        if (action_scope.register_bool_return)
        {
            break;
        }
        // 为找到分支则继续查找
        else
        {
            block_id++;
        }
    }
    // 若当前分支索引是合理的索引，则进入对应分支，执行里面的语句
    if (block_id < n)
    {
        mysh::MyShellScriptExecutor::executeSeriesStatements(action_scope, if_else_statement_block->statements[block_id]);
    }
}

// 执行for-in语句块
void mysh::MyShellScriptExecutor::executeForInStatementBlock(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node)
{
    // to do
}

// 执行for-to语句块
void mysh::MyShellScriptExecutor::executeForToStatementBlock(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::GrammarNode>& node)
{
    // for-to语句块
    auto for_to_block = std::static_pointer_cast<mysh::ForToStatementBlock>(node);
    // to前节点是否需要被赋值
    bool if_assign_before_to = false;
    // to前节点为左值
    if (for_to_block->node_before_to->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::LEFT_VALUE_NODE)
    {
        auto before_to = std::static_pointer_cast<mysh::LeftValueNode>(for_to_block->node_before_to);
        // 检查是否已声明
        if (!action_scope.declared_variables.count(before_to->identifier))
        {
            throw mysh::ExecutorException("\033[31mfor-to语句块中的to前节点尚未声明\033[0m");
        }
        else
        {
            mysh::WORD_TYPE value_type = action_scope.declared_variables[before_to->identifier].data_type;
            std::vector<std::int32_t> indexes = mysh::MyShellScriptExecutor::parseLeftValueNode(action_scope, before_to);
            if (!indexes.empty())
            {
                // to do
            }
            else
            {
                if (value_type == mysh::WORD_TYPE::INTEGER)
                {
                    if (!action_scope.int_values.count(before_to->identifier))
                    {
                        throw mysh::ExecutorException("\033[31mfor-to语句块中的to前节点尚未定义\033[0m");
                    }
                    else
                    {
                        action_scope.register_int_return = action_scope.int_values[before_to->identifier];
                    }
                }
                else
                {
                    throw mysh::ExecutorException("\033[31mfor-to语句块中的to前节点数据类型不是整型\033[0m");
                }
            }
            if_assign_before_to = true;
        }
    }
    // to前节点为数值节点
    else if (for_to_block->node_before_to->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::DATA_NODE)
    {
        auto before_to = std::static_pointer_cast<mysh::DataNode>(for_to_block->node_before_to);
        mysh::MyShellScriptExecutor::parseDataNode(action_scope, before_to);
    }
    // to前节点的数值
    std::int32_t before_to_data = action_scope.register_int_return;
    // to后节点为左值
    if (for_to_block->node_after_to->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::LEFT_VALUE_NODE)
    {
        auto after_to = std::static_pointer_cast<mysh::LeftValueNode>(for_to_block->node_after_to);
        // 检查是否已声明
        if (!action_scope.declared_variables.count(after_to->identifier))
        {
            throw mysh::ExecutorException("\033[31mfor-to语句块中的to后节点尚未声明\033[0m");
        }
        else
        {
            mysh::WORD_TYPE value_type = action_scope.declared_variables[after_to->identifier].data_type;
            std::vector<std::int32_t> indexes = mysh::MyShellScriptExecutor::parseLeftValueNode(action_scope, after_to);
            if (!indexes.empty())
            {
                // to do
            }
            else
            {
                if (value_type == mysh::WORD_TYPE::INTEGER)
                {
                    if (!action_scope.int_values.count(after_to->identifier))
                    {
                        throw mysh::ExecutorException("\033[31mfor-to语句块中的to后节点尚未定义\033[0m");
                    }
                    else
                    {
                        action_scope.register_int_return = action_scope.int_values[after_to->identifier];
                    }
                }
                else
                {
                    throw mysh::ExecutorException("\033[31mfor-to语句块中的to后节点数据类型不是整型\033[0m");
                }
            }
        }
    }
    // to后节点为数值节点
    else if (for_to_block->node_after_to->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::DATA_NODE)
    {
        auto after_to = std::static_pointer_cast<mysh::DataNode>(for_to_block->node_after_to);
        mysh::MyShellScriptExecutor::parseDataNode(action_scope, after_to);
    }
    // to后节点为运行语句
    else if (for_to_block->node_after_to->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::RUN_STATEMENT)
    {
        auto after_to = std::static_pointer_cast<mysh::RunStatementNode>(for_to_block->node_after_to);
        if (after_to->data_type != mysh::WORD_TYPE::INTEGER)
        {
            throw mysh::ExecutorException("\033[31mfor-to语句块中的to后节点数据类型不是整型\033[0m");
        }
        else
        {
            mysh::MyShellScriptExecutor::executeRunStatement(action_scope, after_to);
        }
    }
    // to后节点为调用语句
    else if (for_to_block->node_after_to->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::CALL_STATEMENT)
    {
        // to do
    }
    // to后节点的数值
    std::int32_t after_to_data = action_scope.register_int_return;
    // 开始执行for-to语句块
    for (std::int32_t i = before_to_data; i <= after_to_data; i++)
    {
        // to前节点赋值
        if (if_assign_before_to)
        {
            auto before_to = std::static_pointer_cast<mysh::LeftValueNode>(for_to_block->node_before_to);
            action_scope.int_values[before_to->identifier] = i;
        }
        mysh::MyShellScriptExecutor::executeSeriesStatements(action_scope, for_to_block->statements);
        // 判断to前节点是否超过了to后节点，超过了则直接退出循环
        if (if_assign_before_to)
        {
            auto before_to = std::static_pointer_cast<mysh::LeftValueNode>(for_to_block->node_before_to);
            i = action_scope.int_values[before_to->identifier];
        }
    }
}

// 解析数值节点，将数值存储到对应的寄存器
void mysh::MyShellScriptExecutor::parseDataNode(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::DataNode>& data_node)
{
    if (data_node->data_type == mysh::WORD_TYPE::INTEGER)
    {
        try
        {
            action_scope.register_int_return = std::stoi(data_node->value);
        }
        catch(const std::exception& e)
        {
            throw mysh::ExecutorException("\033[31m无法将整型右值存储到整型寄存器中, 错误信息: " + std::string(e.what()) + "\033[0m");
        }
    }
    else if (data_node->data_type == mysh::WORD_TYPE::BOOL_VALUE)
    {
        if (data_node->value == "true" || data_node->value == "1")
        {
            action_scope.register_bool_return = true;
        }
        else if (data_node->value == "false" || data_node->value == "0")
        {
            action_scope.register_bool_return = false;
        }
        else
        {
            throw mysh::ExecutorException("\033[31m无法将布尔型右值存储到布尔型寄存器中, 请检查布尔型右值代码是否正确\033[0m");
        }
    }
    else
    {
        action_scope.register_string_return = data_node->value;
    }
}

// 解析左值，获取索引数组
std::vector<std::int32_t> mysh::MyShellScriptExecutor::parseLeftValueNode(mysh::MyShellActionScope& action_scope, const std::shared_ptr<mysh::LeftValueNode>& left_value_node)
{
    std::vector<std::int32_t> indexes = std::vector<std::int32_t>();
    for (auto i : left_value_node->index)
    {
        if (i.word_type == mysh::WORD_TYPE::IDENTIFIER)
        {
            if (!action_scope.declared_variables.count(i.name))
            {
                throw mysh::ExecutorException("\033[31m数组值索引中的标识符尚未声明\033[0m");
            }
            else
            {
                if (action_scope.declared_variables[i.name].data_type != mysh::WORD_TYPE::INTEGER)
                {
                    throw mysh::ExecutorException("\033[31m数组值索引中的标识符的值不是整型\033[0m");
                }
                else if (!action_scope.int_values.count(i.name))
                {
                    throw mysh::ExecutorException("\033[31m数组值索引中的标识符尚未定义\033[0m");
                }
                else
                {
                    indexes.push_back(action_scope.int_values[i.name]);
                }
            }
        }
        else
        {
            try
            {
                indexes.push_back(std::stoi(i.name));
            }
            catch(const std::exception& e)
            {
                throw mysh::ExecutorException("\033[31m数组值索引无法转为整型, 错误信息: " + std::string(e.what()) + "\033[0m");
            }                    
        }
    }
    return indexes;
}

// 执行脚本
void mysh::MyShellScriptExecutor::executeScript(const std::vector<std::shared_ptr<mysh::GrammarNode>>& grammar_nodes)
{
    mysh::MyShellActionScope action_scope = mysh::MyShellActionScope();
    mysh::MyShellScriptExecutor::executeSeriesStatements(action_scope, grammar_nodes);
}